/***************************************************************************
 *   Copyright (C) 2008 by Luca Lanziani   *
 *   luca@tulug.it   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "operation.h"

void start_routine(void* paramiter) {
	struct thread_arg *arg=(struct thread_arg*) paramiter;
	get_connection(arg,login(arg));
}

connection_p login(struct thread_arg *arg) {
	char UserName[USERNAMELEN];
	send(arg->thread_socket_id,_("UserName: "),sizeof(_("Username: ")),0);
	recv(arg->thread_socket_id,UserName,USERNAMELEN,0);
		
	arg->thread_id=pthread_self();
	sock_info* sockinfo=new_sock_info(arg->thread_socket_id,arg);
	user_info* userinfo=new_user(UserName,0);
	connection_p connection_info=new_connection(userinfo,sockinfo);
	add_connection(connection_info);
	return connection_info;
	
}

void get_connection(struct thread_arg *arg,connection_p connection) {
	char message[MESSAGELEN];
	send(arg->thread_socket_id,_("Welcome"),sizeof(_("Welcome")),0);
	login_message(connection);
	
	while (1) {
		if (0==recv(arg->thread_socket_id,message,sizeof(message),0)) {
			logout_message(connection);
			close_connection(connection);
		} else {
			sendmessage_all(connection,message);
		}
		
	}
}
	
void login_message(connection_p connection){
	char loginmess[MESSAGELEN];
	memset(loginmess,'\0',sizeof(loginmess));
	snprintf(loginmess,MESSAGELEN,"Login: %s",connection->user->nick);
	sendmessage_all(connection,loginmess);	
}

void logout_message(connection_p connection) {
	char logoutmess[MESSAGELEN];
	memset(logoutmess,'\0',sizeof(logoutmess));
	snprintf(logoutmess,MESSAGELEN,"Logout: %s",connection->user->nick);
	sendmessage_all(connection,logoutmess);	
}

void sendmessage_all(connection_p connection,char *message) {
	connection_p connection1;
	connection_p connection2;
	/* To notify login at other peaple  */
	connection1=connection_list.head;
	connection2=NULL;
	do {
		pthread_mutex_lock(&connection1->m_connection);
		send(connection1->sock->socket_id,connection->user->nick,sizeof(connection->user->nick),0);
		send(connection1->sock->socket_id,message,sizeof(message),0);
		connection2=connection1;
		connection1=connection1->next;
		if (connection2!=NULL) {
			pthread_mutex_lock(&connection1->m_connection);
			pthread_mutex_unlock(&connection2->m_connection);
		}
		
	} while(connection1!=NULL);
}
